/*
*创建时间：2017.3.13
*
*模块开发者：潘克 李冬雪
*
*模块状态：开发中
*/
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//var authtest = require('./middlewares/authhs256/authtest')


var routes = require('./routes');
var app = express();



// view engine setup  use html for coding
app.set('views', path.join(__dirname, 'views'));
app.engine('.html', require('ejs').__express);  
app.set('view engine', 'html');
 
//设置跨域访问 。  该方法设置后  chrome 浏览器出现问题
// app.all('*', function (req, res, next) {
//     res.header("Access-Control-Allow-Origin", "http://localhost:2000");
//     res.header("Access-Control-Allow-Headers", "X-Requested-With,Authorization");
//     res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE");
//     res.header("X-Powered-By", '3.2.1');
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
// });
app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With');
    res.header('Access-Control-Allow-Methods','PUT,POST,GET,DELETE,OPTIONS');
    res.header('X-Powered-By',' 3.2.1');
    if(req.method=='OPTIONS') res.sendStatus(200);/*让options请求快速返回*/
    else  next();
});


// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//自定义中间件  jwt 验证
// app.use(authtest.checkLogin);

// 路由
routes(app);
module.exports = app;